package test1;

public class Test8 {
    public static void main(String[] args) {
        int[] nums = new int[]{2, 5, 4, 6, 1, 7, 6};
        int[] arr = quick_sort(nums, 0, nums.length - 1);
        for (int i : arr) {
            System.out.print(i + " ");
        }
    }

    private static int[] quick_sort(int[] nums, int begin, int end) {
        if (begin > end) {
            return nums;
        }

        int pre = begin;
        int cur = begin + 1;
        int key = begin;

        while (cur <= end) {
            if (nums[cur] < nums[key] && ++pre != cur) {
                swap(nums, pre, cur);
            }
            cur++;
        }
        int meet = pre;
        swap(nums, key, meet);
        quick_sort(nums, begin, meet - 1);
        quick_sort(nums, meet + 1, end);
        return nums;
    }

    private static void swap(int[] nums, int pre, int cur) {
        int tmp = nums[pre];
        nums[pre] = nums[cur];
        nums[cur] = tmp;
    }
}
